package com.aaa.gs.controller;

import com.aaa.common.entity.ProductReview;
import com.aaa.common.vo.ProductReviewVO;
import com.aaa.gs.service.ProductReviewService;

import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import com.aaa.common.util.BaseUtil;
import com.aaa.common.vo.Page;
import com.aaa.common.util.Result;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (ProductReview)表控制层
 *
 * @author makejava
 * @since 2025-09-16 09:36:37
 */
@RestController
@RequestMapping("productReview")
@Log4j2
public class ProductReviewController {
    /**
     * 服务对象
     */
    @Resource
    private ProductReviewService productReviewService;

    /**
     * 查询商品评论数量和好评率
     */
    @PostMapping("queryReviewCount")
    public Result queryReviewCount(Integer productId) {
        return BaseUtil.success(this.productReviewService.queryCountAll(productId));
    }

    /**
     * 通过评价类型查询评价数量
     * @param 
     * @return 评价数量
     */
    @PostMapping("queryReviewTypeCount")
    public Result queryReviewTypeCount(Integer productId) {
        return BaseUtil.success(this.productReviewService.queryCountAll(productId));
    }

    /**
     * 查询商品评价带图片数量
     * @param productId
     * @return 评价图片数量
     */
    @PostMapping("queryReviewImageCount")
    public Result queryReviewImageCount(Integer productId) {
        List<ProductReviewVO> imageList = this.productReviewService.queryImageCount(productId);
        int totalCount = imageList.size();
        /*List<Object> result = new ArrayList<>();
        result.add(totalCount);
        result.add(productReviewVOList);*/
        // 构建返回数据（使用Map更清晰）
        Map<String, Object> result = new HashMap<>();
        result.put("totalCount", totalCount);
        result.put("imageList", imageList);
        return BaseUtil.success(result);
    }

    /**
     * 查询商品评价
     * @param productReviewVO 查询对象
     * @return 查询结果
     */
    @PostMapping("queryReview")
    public Result queryReview(@RequestBody ProductReviewVO productReviewVO) {
        Page<ProductReviewVO> productReviewVOPage = new Page<>();
        productReviewVOPage.setPageNo(1);
        productReviewVOPage.setPageSize(1000);
        productReviewVOPage.setData(productReviewVO);
        return BaseUtil.success(this.productReviewService.queryByPage(productReviewVOPage));
    }


    /**
     * 分页查询
     * @param page 分页对象
     * @return 查询结果
     */
    @PostMapping("query")
    public Result queryByPage(@RequestBody Page<ProductReviewVO> page) {
        return BaseUtil.success(this.productReviewService.queryByPage(page));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("queryById")
    public Result queryById(Integer id) {
        return BaseUtil.success(this.productReviewService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param productReview 实体
     * @return 新增结果
     */
    @PostMapping("insert")
    public Result add(@RequestBody ProductReview productReview) {
        return BaseUtil.success(this.productReviewService.insert(productReview));
    }

    /**
     * 编辑数据
     *
     * @param productReview 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    public Result edit(@RequestBody ProductReviewVO productReview) {
        return BaseUtil.success(this.productReviewService.update(productReview));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer id) {
        return BaseUtil.success(this.productReviewService.deleteById(id));
    }

}

